ihelpuoo.namespace("ihelpuoo.widget.date");

ihelpuoo.widget.date.DateView = function(OnChange,gasket,name,patternName,OnDblClick)
{
	if(!name)
	{
		ihelpuoo.error("You must define a unique name for DateView!");
	}
	this.view = new ihelpuoo.widget.View(gasket,(patternName?patternName:"eve.pattern.date.Normal"));
	this.name = name;
	ihelpuoo.widget.registerView2(this);
	this.date = null;
	this.oYear = null;
	this.oMonth = null;
	this.oDay = null;
	this.oToday = null;
	this.oClose = null;
	this.OnChange = OnChange;
	this.OnDblClick = OnDblClick;
}

ihelpuoo.widget.date.DateView.prototype
=
{
	setGasket: function(gasket)
	{
		this.view.gasket = gasket;
	}
	
	,
	initView: function()
	{
		if(this.view.isNeverBeDrawn())
		{
			this.view.preparePattern();
			this.oYear = this.view.getElement("year");
			this.oMonth = this.view.getElement("month");
			this.oDay = this.view.getElement("day");
			this.oClose = this.view.getElement("close");
			
			ihelpuoo.widget.addViewEvent(this,this.oYear,"keyup",this.onKeyChangeYear);
			ihelpuoo.widget.addViewEvent(this,this.oYear,"change",this.onChangeYear);
			ihelpuoo.widget.addViewEvent(this,this.oMonth,"change",this.onChangeMonth);
			ihelpuoo.widget.addViewEvent(this,this.oYear,"click",this.onClickYearOrMonth);
			ihelpuoo.widget.addViewEvent(this,this.oMonth,"click",this.onClickYearOrMonth);
			
			for(var i=7;i<49;i++)
			{
				var cell = ihelpuoo.dom.getCellInTable(this.oDay,i);
				cell.title = "Cell: " + (i-7);
				cell.setAttribute("index",(i-7));
				ihelpuoo.widget.setViewNameToElement(cell,this);
				ihelpuoo.event.add(cell,"click",this.onChangeDay);
				if(this.OnDblClick)
					ihelpuoo.event.add(cell,"dblclick",this.onDbClickCell);
			}
			
			if(this.oClose)
			{
				ihelpuoo.widget.setViewNameToElement(this.oClose,this);
				ihelpuoo.event.add(this.oClose,"click",this.onClose);
			}
		}
	}
	
	,
	draw: function(date)
	{
		if(date)
			this.date = date.clone();
		if(!this.date)
			return;
		this.initView();
		this.view.display();
		this.clear();
		if(this.date)
		{
			ihelpuoo.dom.autoSetObject(this.oYear,this.date.year);
			ihelpuoo.dom.autoSetObject(this.oMonth,this.date.month);
			var first = 7 + this.date.getMonthFirstWeekDay();
			var max = this.date.getMaxDayOfMonth();
			for(var i=7;i<first;i++)
			{
				var cell = ihelpuoo.dom.getCellInTable(this.oDay,i);
				cell.className = "eve_date_item_blank"
			}
			for(var i=0;i<max;i++)
			{
				var cell = ihelpuoo.dom.getCellInTable(this.oDay,i+first);
				var d = i+1;
				cell.innerHTML = d;
				if(d == this.date.day)
				{
					this.oToday = cell;
					cell.className = "eve_date_item_current";
				}
			}
			for(var i = max+first;i<49;i++)
			{
				var cell = ihelpuoo.dom.getCellInTable(this.oDay,i);
				cell.className = "eve_date_item_blank"
			}
		}
	}
	
	,
	onClickYearOrMonth: function()
	{
		ihelpuoo.event.cancelBubble(ihelpuoo.Event(arguments));
	}
	
	,
	onDbClickCell: function()
	{
		var v = ihelpuoo.widget.getViewByEventSource(this);
		if(v.OnDblClick)
			v.OnDblClick(v);
	}
	
	,
	onClose: function()
	{
		var v = ihelpuoo.widget.getViewByEventSource(this);
		v.hide();
	}
	
	,
	changeDay: function(src)
	{
		if(!src) return;
		var first = this.date.getMonthFirstWeekDay();	// 1 base;
		var max = this.date.getMaxDayOfMonth()+first;	// 1 base;
		var index = parseInt(src.getAttribute("index")); // 0 base
		
		if(index<first || index>=max)  return;
	
		this.oToday.className = "eve_date_item";
		this.oToday = src;
		this.oToday.className = "eve_date_item_current";
		this.date.setDay(ihelpuoo.parseInt(this.oToday.innerHTML));
	}
	
	,onChangeDay: function()
	{
		ihelpuoo.event.cancelBubble(ihelpuoo.Event(arguments));
		var src = ihelpuoo.event.srcElement(this);
		var v = ihelpuoo.widget.getViewByElement(src);
		v.changeDay(src);
		if(v.OnChange)
			v.OnChange(v);
	}
	
	,
	onKeyChangeYear: function()
	{
		var v = ihelpuoo.widget.getViewByEventSource(this);
		if(ihelpuoo.isEmpty(v.oYear.value))return;
		v.date.setYear(ihelpuoo.parseInt(v.oYear.value,1970));
		if(v.date.year<1) v.date.setYear(1970);
		v.draw();
		if(v.OnChange)
			v.OnChange(v);
	}
	
	,
	onChangeYear: function()
	{
		var v = ihelpuoo.widget.getViewByEventSource(this);
		v.date.setYear(ihelpuoo.parseInt(v.oYear.value,1970));
		if(v.date.year<1) v.date.setYear(1970);
		v.draw();
		if(v.OnChange)
			v.OnChange(v);
	}
	
	,
	onChangeMonth: function()
	{
		var v = ihelpuoo.widget.getViewByEventSource(this);
		v.date.setMonth(ihelpuoo.parseInt(v.oMonth.value));
		v.draw();
		if(v.OnChange)
			v.OnChange(v);
	}
	
	,
	clear: function()
	{
		for(var i=1;i<this.oDay.rows.length;i++)
		{
			var row = this.oDay.rows[i];
			for(var j=0;j<row.cells.length;j++)
			{
				var cell = row.cells[j];
				cell.innerHTML = "&nbsp;";
				cell.className = "eve_date_item";
			}
		}
	}

	,
	getDate: function()
	{
		return this.date;
	}
	
	// <WEBOO:INHERIT extends="ihelpuoo.widget.View">
	,getElement: function(key){return this.view.getElement(key);}
	,isNeverBeDrawn: function(){return this.view.isNeverBeDrawn();}
	,preparePattern: function(){this.view.preparePattern();}
	,display: function(){this.view.display();}
	,hide: function(){this.view.hide();}
	// </WEBOO:INHERIT>
}